package com.chaoli.reggie_diy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chaoli.reggie_diy.common.R;
import com.chaoli.reggie_diy.domain.Category;
import com.chaoli.reggie_diy.domain.Orders;
import com.chaoli.reggie_diy.domain.Setmeal;
import com.chaoli.reggie_diy.dto.SetmealDto;
import com.chaoli.reggie_diy.service.CategoryService;
import com.chaoli.reggie_diy.service.OrderService;
import com.chaoli.reggie_diy.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增套餐
     *
     * @param setmealDto 扩展Setmeal的属性
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> add(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto.toString());
        setmealService.saveSetmealAndSetmealDish(setmealDto);
        return R.success("添加套餐成功!!");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(@RequestParam int page, @RequestParam int pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name)
                .orderByDesc(Setmeal::getUpdateTime);
        //从setmeal表进行分页查询
        setmealService.page(setmealPage, queryWrapper);
        //将setmealPage的分页数据拷贝给setmealDto的分页数据
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> setmealList = setmealPage.getRecords();
        //给setmealDto的records属性赋值
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            //将setmeal的属性赋值给setmealDto
            BeanUtils.copyProperties(setmeal, setmealDto);
            //setmealDto对象赋值categroyName属性
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    /**
     * 删除单个套餐或者多个套餐
     * <p>
     * value：设置缓存的名字
     * allEntries：清理所有的缓存数据
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
        //删除套餐的同时把setmeal_dish表中的对应数据也要删除
        setmealService.removeSetmealAndDish(ids);
        return R.success("删除成功！！");
    }

    /**
     * 查询所有的套餐数据
     * <p>
     * 因为前端传来的是两个Setmeal中的数据
     * 一开始是用@ResquestParam接收，只能用于接收一个参数
     * 这里是两个请求参数
     * 要么不加注解，用实体类接收
     * 要么用两个@ResquestParam注解接收参数
     * <p>
     * value：设置缓存的名字
     * key：设置缓存的key
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {

        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != null, Setmeal::getCategoryId, categoryId)
                .eq(status != null, Setmeal::getStatus, 1);
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        if (setmealList == null) {
            return R.error("没有查询到套餐");
        }
        return R.success(setmealList);

    }

    /**
     * 套餐的启售和停售
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status, @RequestParam("ids") List<Long> ids) {
        setmealService.updateSetmealStatusByIds(status, ids);
        return R.success("更新套餐售卖状态成功!!");
    }

    /**
     * 根据套餐id查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getData(@PathVariable("id") Long id) {
        SetmealDto setmealDto = setmealService.getData(id);
        return R.success(setmealDto);
    }

    /**
     * 套餐管理的修改
     *
     * @param setmealDto
     * @return
     */
    @PutMapping()
    public R<String> edit(@RequestBody SetmealDto setmealDto) {
        return setmealService.edit(setmealDto);

    }



}
